package com.yummy.cloud.gateway.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;

import com.yummy.cloud.gateway.manage.dao.mapper.RouteDefinitionPlainMapper;
import com.yummy.cloud.gateway.route.GatewayRouteWatch;
import com.yummy.cloud.gateway.route.MysqlRouteDefinitionRepository;

@Configuration
@EnableConfigurationProperties(RouteWatchProperties.class)
public class GatewayConfiguration {

	@Bean
	public MysqlRouteDefinitionRepository mysqlRouteDefinitionRepository(
			RouteDefinitionPlainMapper routeDefinitionPlainMapper) {
		MysqlRouteDefinitionRepository repository = new MysqlRouteDefinitionRepository(routeDefinitionPlainMapper);
		return repository;
	}

	@Bean
	@ConditionalOnProperty(value = "spring.cloud.gateway.route.watch.enabled",
			matchIfMissing = true)
	public GatewayRouteWatch gatewayRouteWatch(RouteWatchProperties routeWatchProperties,
			ObjectProvider<TaskScheduler> taskScheduler) {
		return new GatewayRouteWatch(routeWatchProperties, taskScheduler);
	}
	
	
}